#!/usr/bin/env python
# -*- coding: utf-8 -*-

import matplotlib.lines as lines
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import rcParams

rcParams['font.family'] = 'sans-serif'
rcParams['font.sans-serif'] = ['HarmonyOS Sans SC', 'Microsoft YaHei']
rcParams['font.size'] = 24

data = (
    {
        'xlab': '负载轨迹',
        'group': ('LRU', 'Mithril', 'Emith'),
        'x': ('MSR0', 'MSR1', 'MSN1', 'MSN2', 'MSN3', 'WEB1', 'WEB2', 'WEB3'),
        'hr': (
            (0.302468, 0.020020, 0.681697, 0.707732, 0.772707, 0.929128, 0.932954, 0.939695),
            (0.353232, 0.033164, 0.833675, 0.840472, 0.866939, 0.953206, 0.959171, 0.968541),
            # (0.340390, 0.029634, 0.751190, 0.765236, 0.798901, 0.952265, 0.953273, 0.956663),
            (0.364780, 0.036439, 0.916873, 0.875839, 0.898129, 0.967107, 0.967229, 0.968006),
        ),
        # 'hr_min': (0.399245, 0.057670, 0.917349, 0.895532, 0.925870, 0.970079, 0.967856, 0.977068),
        'hrlim': (0, 100),
        'color': ('tab:blue', 'tab:orange', 'tab:green'),
        'hatch': ('', '', ''),
        'output_hr': 'figures/Fig_MSR_hr1.png',
    },
    {
        'xlab': '负载轨迹',
        'group': ('LRU', 'Mithril', 'Emith'),
        'x': ('WEB4', 'WEB5', 'WEB6', 'WEB7', 'WEB8', 'WEB9', 'WEB10', 'WEB11'),
        # 'hr_lru':
        'hr': (
            (0.951224, 0.93937, 0.935799, 0.900462, 0.895733, 0.968996, 0.917453, 0.923266),
            (0.966981, 0.960074, 0.944953, 0.920316, 0.915814, 0.973303, 0.937840, 0.938308),
            # (0.964718, 0.953899, 0.943912, 0.921128, 0.913417, 0.972947, 0.927354, 0.934206),
            (0.967264, 0.964147, 0.955318, 0.930069, 0.916446, 0.973113, 0.951332, 0.950256),
        ),
        # 'hr_min': (0.968075, 0.968415, 0.961026, 0.946371, 0.918209, 0.985453, 0.957682, 0.960710),
        'hrlim': (75, 100),
        'color': ('tab:blue', 'tab:orange', 'tab:green'),
        'hatch': ('', '', ''),
        'output_hr': 'figures/Fig_MSR_hr2.png',
    },
)


def main_hr(
    group=(),  color=('1', '1', '0.5'), hatch=('', '//', ''),
    hr=(),  hr_lru=None, hr_min=None, hrlim=None,
    x=None, xlab='', xwidth=0.27,
    output_hr=None, **kwargs
):
    fig = plt.figure(figsize=(14, 9))  # create figure
    ax = plt.axes(position=[0.085, 0.11, 0.9, 0.78])  # create axes

    N = len(x)
    G = len(group)
    X = np.array(range(N), dtype=float)

    nhr = np.array(hr, dtype=float) * 100  # to 100%

    for i in range(G):
        style_hr = {'color': color[i], 'hatch': hatch[i], 'edgecolor': 'k', 'linewidth': 2.5}
        ax.bar(X + xwidth * i, nhr[i, :], xwidth, label=group[i], ** style_hr)

    if hr_lru:
        lru_x = []
        lru_y = []
        for i in range(N):
            h = hr_lru[i] * 100
            lru_x.extend([i - xwidth * 0.7, i + xwidth * (G-0.3), None])
            lru_y.extend([h, h, None])
        ax.plot(lru_x, lru_y, label='LRU', linewidth=3.5, color='k')

    if hr_min:
        min_x = []
        min_y = []
        for i in range(N):
            h = hr_min[i] * 100
            min_x.extend([i - xwidth * 0.7, i + xwidth * (G-0.3), None])
            min_y.extend([h, h, None])
        ax.plot(min_x, min_y, '--', label='MIN', linewidth=3, color='k')

    if hrlim:
        ax.set_ylim(hrlim)
    ax.set_ylabel('缓存命中率 (%)')

    ax.set_xticks(X + xwidth * (G-1) / 2)
    if x:
        ax.set_xticklabels(x)

    ax.set_xlabel(xlab)
    ax.legend(bbox_to_anchor=(0.49, 1.125), loc=9, ncol=5, edgecolor='1')

    if type(output_hr) == str:
        if False:
            fig.add_artist(lines.Line2D([0, 1], [0, 1]))
            fig.add_artist(lines.Line2D([0, 1], [1, 0]))
            fig.add_artist(lines.Line2D([0, 0, 1, 1, 0], [0, 1, 1, 0, 0]))
        plt.savefig(output_hr)
    pass


def main_tm(
    group=(),  color=('1', '1', '0.5'), hatch=('', '//', ''),
    tm=(), tm_cnt=1000000, tmlim=None,
    x=None, xlab='', xwidth=0.27,
    output_tm=None, **kwargs
):
    fig = plt.figure(figsize=(14, 9))  # create figure
    ax = plt.axes(position=[0.1, 0.11, 0.88, 0.78])  # create axes

    N = len(x)
    G = len(group)
    X = np.array(range(N), dtype=float)

    data = tm_cnt / 1000 / np.array(tm, dtype=float)
    print(data)

    for i in range(G):
        style_hr = {'color': color[i], 'hatch': hatch[i], 'edgecolor': 'k', 'linewidth': 2.5}
        ax.bar(X + xwidth * i, data[i, :], xwidth, label=group[i], ** style_hr)

    if tmlim:
        ax.set_ylim(tmlim)
    ax.set_ylabel('运行速度 (KOPS/s)')

    ax.set_xticks(X + xwidth * (G-1) / 2)
    if x:
        ax.set_xticklabels(x)

    ax.set_xlabel(xlab)
    ax.legend(bbox_to_anchor=(0.49, 1.125), loc=9, ncol=5, edgecolor='1')

    if type(output_tm) == str:
        if False:
            fig.add_artist(lines.Line2D([0, 1], [0, 1]))
            fig.add_artist(lines.Line2D([0, 1], [1, 0]))
            fig.add_artist(lines.Line2D([0, 0, 1, 1, 0], [0, 1, 1, 0, 0]))
        plt.savefig(output_tm)
    pass


# main_hr(**data[0])
# main_tm(**data[0])
main_hr(**data[0])
main_hr(**data[1])
